using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace NET.Library.Data
{
    /// <summary>
    /// 
    /// </summary>
    public class DBErrorHandler
    {
        private static string _EventSource = "CustomProfileProvider";
        private static string _EventLog = "Application";
        //private static string _ExceptionMessage = "An exception occurred. Please check the event log.";
        
        /// <summary>
        /// WriteToEventLog
        /// A helper function that writes exception detail to the event 
        /// log. Exceptions are written to the event log as a security 
        /// measure to prevent private database details from being 
        /// returned to the browser. If a method does not return a 
        /// status or Boolean value indicating whether the action succeeded 
        /// or failed, the caller also throws a generic exception. 
        /// </summary>
        /// <param name="e"></param>
        /// <param name="action"></param>
        public static void WriteToEventLog(Exception e, string action)
        {
            EventLog log = new EventLog();
            try
            {
                log.Source = _EventSource;
                log.Log = _EventLog;

                string message = "An exception occurred while communicating with the data source.\n\n";
                message += "Action: " + action + "\n\n";
                message += "Exception: " + e.ToString();

                log.WriteEntry(message);
            }
            finally
            {
                log.Dispose();
            }
        }
    }
}
